#ifndef SPREADSHEET_H
#define SPREADSHEET_H

#include "QTableWidget.h"

class Cell;
class SpreadsheetCompare;

class Spreadsheet : public QTableWidget
{
	Q_OBJECT
public:
	Spreadsheet(QWidget *parent = 0);
	Spreadsheet(int rows,int column,QWidget* parent=0);
	~Spreadsheet();

public:
	bool autoRecalculate() const{return autoRecalc;}
	QString currentLocation() const;
	QString currentFormula() const;
	QTableWidgetSelectionRange selectedRange() const;
	void clear();
	bool readFile(const QString& fileName);
	bool writeFile(const QString& fileName);
	void sort(const SpreadsheetCompare& compare);

public Q_SLOTS:
	void cut();
	void copy();
	void paste();
	void del();
	void selectCurrentRow();
	void selectCurrentColumn();
	void recalculate();
	void findNext(const QString& str,Qt::CaseSensitivity cs);

Q_SIGNALS:
	void modified();
private:
	enum {MagicNumber=0x7F51C882,RowCount=999,ColumnCount=20};
	Cell *cell(int row,int column) const;
	QString text(int row,int column) const;
	QString formula(int row,int column) const;
	void setFormula(int row,int column,const QString& formula);
	bool autoRecalc;

private Q_SLOTS:
	void somethingChanged();

};

class SpreadsheetCompare
{
public:
	bool operator()(const QStringList& row1,const QStringList row2)const;
	enum{KeyCount=3};
	int keys[KeyCount];
	bool ascending[KeyCount];
};



#endif